package com.suzj.extraenchantments.util;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class EnchantmentUtil {
    public static ArrayList<ItemStack> findEnchantedItems(Player player, Enchantment enchantment) {
        ArrayList<ItemStack> list = new ArrayList<>();
        ItemStack mainHandItem = player.getMainHandItem();
        if (checkItemForEnchantment(mainHandItem, enchantment)) {
            list.add(mainHandItem);
        }
        ItemStack offHandItem = player.getOffhandItem();
        if (checkItemForEnchantment(offHandItem, enchantment)) {
            list.add(offHandItem);
        }
        for (ItemStack armorItem : player.getArmorSlots()) {
            if (checkItemForEnchantment(armorItem, enchantment)) {
                list.add(armorItem);
            }
        }
        Iterator it = player.getInventory().items.iterator();
        while (it.hasNext()) {
            ItemStack itemStack = (ItemStack) it.next();
            if (checkItemForEnchantment(itemStack, enchantment)) {
                list.add(itemStack);
            }
        }
        return list;
    }

    public static boolean checkItemForEnchantment(ItemStack itemStack, Enchantment enchantment) {
        return !itemStack.isEmpty() && EnchantmentHelper.getItemEnchantmentLevel(enchantment, itemStack) > 0;
    }

    public static boolean isNormalDamageType(DamageSource damageSource) {
        var damageSourceList= List.of("player","mob","sting","arrow","trident","fireworks","onFire","witherSkull","thrown","indirectMagic","thorns","explosion.player","fall");
        if (damageSourceList.contains(damageSource.getMsgId())){
            return true;
        }

        return false;
    }

    public static boolean isMoving(LivingEntity entity, double x, double y, double z) {
        return (entity.getX() == x && entity.getY() == y && entity.getZ() == z) ? false : true;
    }

    public static int getArmorsTotalLevelsWithEnchantment(LivingEntity entity, Enchantment enchantment) {
        int sum = 0;
        for (ItemStack armorItem : entity.getArmorSlots()) {
            int level = EnchantmentHelper.getItemEnchantmentLevel(enchantment, armorItem);
            sum += level;
        }
        return sum;
    }
}